package com.vavr.checkincms.common.util;

import lombok.extern.slf4j.Slf4j;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.Objects;

@Slf4j
public class HttpUtil {

    private HttpUtil() {
    }

    public static String post(String url,String param) {
        try {
            RestTemplate restTemplate=new RestTemplate();
            HttpEntity<String> userHttpEntity = getStringHttpEntity(param, null);
            return restTemplate.postForEntity(url,userHttpEntity,String.class).getBody();
        } catch (RestClientException e) {
            log.error("提示词获取接口调用失败",e);
        }
        return null;
    }

    public static String post(String url, String param, Map<String,String> headParam) {
        try {
            RestTemplate restTemplate=new RestTemplate();
            HttpEntity<String> userHttpEntity = getStringHttpEntity(param, headParam);
            return restTemplate.postForEntity(url,userHttpEntity,String.class).getBody();
        } catch (RestClientException e) {
            log.error("提示词获取接口调用失败",e);
        }
        return null;
    }

    public static  String get(String url, String param, Map<String,String> headParam) {
        try {
   /*    TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        public X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                    }
            };

            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };*/

            RestTemplate restTemplate=new RestTemplate();
            HttpEntity<String> userHttpEntity = getStringHttpEntity(param, headParam);
/*            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

            HttpsURLConnection con = (HttpsURLConnection) new URL(url).openConnection();
            con.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder content = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            return   content.toString();*/
             return  restTemplate.exchange(url, HttpMethod.GET,userHttpEntity,String.class).getBody();
        } catch (Exception e) {
            log.error("提示词获取接口调用失败",e);
        }

        return null;
    }

    private static HttpEntity<String> getStringHttpEntity(String param, Map<String, String> headParam) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type","application/json;utf-8");
        if(Objects.nonNull(headParam)) {
            headParam.forEach(headers::set);
        }
        return  new HttpEntity<>(param, headers);
    }
}
